﻿package  com.socket
{
	import com.staticInfo.EventConst;
	import flash.events.EventDispatcher;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	import flash .events.Event;
	import com.event.*;
	import com.utils.DebugTrace;
	
	/**
	 * 静态类，用来通讯，负责收发信息
	 * 发送信息都集中在这里
	 * @author zhengzihua
	 */
	public  class LocalCommunications
	{
		private static var mySend:LocalConnection;
		private static var myRec:LocalConnection;
		public static var dispather:EventDispatcher=new EventDispatcher();
		
		/**
		 * 创建通发送和接收信息的链接
		 */
		public static function LocalCommInit():void {
			myRec = new LocalConnection();
			mySend = new LocalConnection();
			myRec.connect("localFarm");
			myRec.client = LocalCommunications;
			mySend.addEventListener(StatusEvent.STATUS, onStatus);
		}	
		private static function onStatus(e:StatusEvent):void 
		{
			switch (e.level) {
				case "status" :
					DebugTrace.dtrace("code info LocalCommunications.as:suc----------------------");
					break;
				case "error" :
					DebugTrace.dtrace("code info LocalCommunications.as:error--------------------");
					break;
				default:
					DebugTrace.dtrace("code error LocalCommunications.as:"+e.level);
			}	
		}
		public  static function farmDeal(str:String):void {
			DebugTrace.dtrace("code info LocalCommunications.as:收到信息\"" + str + "\"");
			var event:EventZheng = new EventZheng(EventConst.EVENT_FROMSERVER);
			event.obj.str = str;
			dispather.dispatchEvent(event);
		}
		public  static function sendMsg(str:String):void {
			try{
				mySend.send("localImitation", "imitationDeal", str);
			}catch (e:Error) {
				DebugTrace.dtrace("error code LocalComm:" + e);
			}
		}
		//发送给服务器的信息
	}
}